/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import DAO.UsuarioDAO;
import Model.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Maury
 */
public class removerUsuario extends Processor {
    
    @Override
    public void execute() throws ServletException, IOException {
        getResponse().setContentType("text/html;charset=UTF-8");
        PrintWriter out = getResponse().getWriter();
        try{
            String login = getRequest().getParameter("login");
            boolean removido = UsuarioDAO.removerUsuario(login);
            
            HttpSession session = getRequest().getSession(false);
            Usuario user = (Usuario) session.getAttribute("usuario");
            session.removeAttribute("destino");
            
            String message;
            if(removido){
                message = "Usuário removido com sucesso!";
            }
            else{
                message = "Usuário não pode ser removido!";
            }
            getRequest().setAttribute("message", message);
            if(login.equals(user.getLogin()) && removido){
                RequestDispatcher rd = getRequest().getRequestDispatcher("MainAccessServlet?cmd=logoutProcessor");                
                rd.forward(getRequest(), getResponse());
            }
            else{
                RequestDispatcher rd = getRequest().getRequestDispatcher("MainAccessServlet?cmd=pesquisarUsuario");          
                rd.include(getRequest(), getResponse());
            }
        }   
        catch(SQLException e){
            throw new ServletException(e); 
        }
        catch(ClassNotFoundException e){
            throw new ServletException(e);
        }
    }
}
